package com.biye.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.base.common.edu.entity.Staff;
import com.base.common.edu.entity.Student;
import com.base.common.edu.mapper.StaffMapper;
import com.base.common.edu.mapper.StudentMapper;
import com.base.common.edu.mapper.ViewMapper;
import com.base.common.edu.util.Page;
import com.base.common.httpStatusCode.enumDefine.StatusCode;
import com.base.common.security.util.EncryptUtil;
import com.biye.api.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private ViewMapper viewMapper;

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private StudentMapper studentMapper;

    @Override
    public JSONObject updateUserInfo(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "操作完成，未进行任何有效操作";
        int statusCode = StatusCode.Failure._statusCode;
        // 判断请求参数的合法性
        if (
                !requestMap.containsKey("user_id")
                || !requestMap.containsKey("user_type")
                || !requestMap.containsKey("recent_province")
                || !requestMap.containsKey("recent_city")
                || !requestMap.containsKey("recent_area")
                || !requestMap.containsKey("recent_street")
                || !requestMap.containsKey("user_name")
                || !requestMap.containsKey("user_sex")
        ) {
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        // 取出请求参数
        String user_id = (String) requestMap.get("user_id");
        String user_type = (String) requestMap.get("user_type");
        String recent_province = (String) requestMap.get("recent_province");
        String recent_city = (String) requestMap.get("recent_city");
        String recent_area = (String) requestMap.get("recent_area");
        String recent_street = (String) requestMap.get("recent_street");
        String user_name = (String) requestMap.get("user_name");
        int user_sex = (int) requestMap.get("user_sex");
        // 判断用户类型
        if ( "stu".equals(user_type) ){
            Student student = new Student();
            student.setStuID(user_id);
            student.setStuName(user_name);
            student.setSex(user_sex);
            student.setProvinceId(recent_province);
            student.setCityId(recent_city);
            student.setAreaId(recent_area);
            student.setStreetInfo(recent_street);
            int i = studentMapper.updateStudent(student);
            if ( i > 0 ) {
                msg = "个人信息修改成功";
                statusCode = StatusCode.Success._statusCode;
            }
        } else if ( "tea".equals(user_type) ) {
            Staff staff = new Staff();
            staff.setStaffId(user_id);
            staff.setName(user_name);
            staff.setSex(user_sex);
            staff.setProvinceId(recent_province);
            staff.setCityId(recent_city);
            staff.setAreaId(recent_area);
            staff.setStreetInfo(recent_street);
            int i = staffMapper.updateSatff(staff);
            if ( i > 0 ) {
                msg = "个人信息修改成功";
                statusCode = StatusCode.Success._statusCode;
            }
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    @Override
    public JSONObject updateUserPassword(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "操作完成，未进行任何有效操作";
        int statusCode = StatusCode.Failure._statusCode;
        // 判断请求参数的合法性
        if (
                !requestMap.containsKey("user_id")
                        || !requestMap.containsKey("user_type")
                        || !requestMap.containsKey("old_pass")
                        || !requestMap.containsKey("new_pass")
        ) {
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        // 取出请求参数
        String user_id = (String) requestMap.get("user_id");
        String user_type = (String) requestMap.get("user_type");
        String old_pass = (String) requestMap.get("old_pass");
        old_pass = EncryptUtil.getMD5String(old_pass);
        String new_pass = (String) requestMap.get("new_pass");
        new_pass = EncryptUtil.getMD5String(new_pass);
        // 判断用户类型
        if ( "stu".equals(user_type) ){
            Student student = studentMapper.queryStudentById(user_id);
            System.out.println(student.getPassword());
            System.out.println(old_pass);
            if ( student != null && old_pass.equals(student.getPassword()) ){
                Student stu_upd = new Student();
                stu_upd.setPassword(new_pass);
                stu_upd.setStuID(user_id);
                int i = studentMapper.updateStudent(stu_upd);
                if ( i > 0 ) {
                    msg = "登录密码修改成功";
                    statusCode = StatusCode.Success._statusCode;
                }
            }
            if ( student == null || !old_pass.equals(student.getPassword()) )
                msg = "原密码验证失败";
        } else if ( "tea".equals(user_type) ) {
            Staff staff = staffMapper.queryStaffById(user_id);
            if ( staff != null && old_pass.equals(staff.getPasswd()) ){
                Staff tea_upd = new Staff();
                tea_upd.setPasswd(new_pass);
                tea_upd.setStaffId(user_id);
                int i = staffMapper.updateSatff(tea_upd);
                if ( i > 0 ) {
                    msg = "登录密码修改成功";
                    statusCode = StatusCode.Success._statusCode;
                }
                // the password '123456' will be '14e1b600b1fd579f47433b88e8d85291' after experienced 'Md5 salt*2'
            }
            if ( staff == null || !old_pass.equals(staff.getPasswd()) )
                msg = "原密码验证失败";
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    @Override
    public JSONObject queryUserInfo(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "暂无数据";
        int statusCode = StatusCode.Failure._statusCode;
        // 判断请求参数的合法性
        if (
                !requestMap.containsKey("user_id")
                        || !requestMap.containsKey("user_type")
        ) {
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        String userType = (String) requestMap.get("user_type");
        String userId = (String) requestMap.get("user_id");
        // data map
        Map<String, Object> dataMap = null;
        // if tea
        if ( "tea".equals(userType) ){
            Staff staff = staffMapper.queryStaffById(userId);
            if ( staff != null ) {
                dataMap = new HashMap<>();
                dataMap.put("user_no", staff.getTno());
                dataMap.put("user_name", staff.getName());
                dataMap.put("user_sex", staff.getSex());
                dataMap.put("user_card", staff.getIdCard());
                dataMap.put("user_recent_p", staff.getProvinceId()==null?"":staff.getProvinceId());
                dataMap.put("user_recent_c", staff.getCityId()==null?"":staff.getCityId());
                dataMap.put("user_recent_a", staff.getAreaId()==null?"":staff.getAreaId());
                dataMap.put("user_recent_t", staff.getStreetInfo()==null?"":staff.getStreetInfo());
                dataMap.put("user_mail", staff.getEmail());
                msg = "查询成功";
                statusCode = StatusCode.Success._statusCode;
                json.put("userinfo", dataMap);
            }
        }
        // if stu
        if ( "stu".equals(userType) ) {
            Student student = studentMapper.queryStudentById(userId);
            if ( student != null ) {
                dataMap = new HashMap<>();
                dataMap.put("user_no", student.getSno());
                dataMap.put("user_name", student.getStuName());
                dataMap.put("user_sex", student.getSex());
                dataMap.put("user_card", student.getIdCard());
                dataMap.put("user_recent_p", student.getProvinceId()==null?"":student.getProvinceId());
                dataMap.put("user_recent_c", student.getCityId()==null?"":student.getCityId());
                dataMap.put("user_recent_a", student.getAreaId()==null?"":student.getAreaId());
                dataMap.put("user_recent_t", student.getStreetInfo()==null?"":student.getStreetInfo());
                dataMap.put("user_mail", student.getEmail());
                msg = "查询成功";
                statusCode = StatusCode.Success._statusCode;
                json.put("userinfo", dataMap);
            }
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    @Override
    public JSONObject queryAllUsersInfo(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "暂无数据";
        int statusCode = StatusCode.Failure._statusCode;
        // 判断请求参数的合法性
        if (
                !requestMap.containsKey("pageNum")
                        || !requestMap.containsKey("pageSize")
                        || !requestMap.containsKey("user_type")
                        || !requestMap.containsKey("user_no")
                        || !requestMap.containsKey("name")
                        || !requestMap.containsKey("id_card")
                        || !requestMap.containsKey("user_sex")
                        || !requestMap.containsKey("user_status")
        ) {
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        // 取出参数
        int pageNum = (int) requestMap.get("pageNum");
        int pageSize = (int) requestMap.get("pageSize");
        String user_type = (String) requestMap.get("user_type");
        String user_no = (String) requestMap.get("user_no");
        String name = (String) requestMap.get("name");
        String id_card = (String) requestMap.get("id_card");
        String user_sex = (String) requestMap.get("user_sex");
        String user_status = (String) requestMap.get("user_status");
        // 对参数进行空参校验
        if ( "".equals(user_type) )
            user_type = null;
        if ( "".equals(user_no) )
            user_no = null;
        if ( "".equals(name) )
            name = null;
        if ( "".equals(id_card) )
            id_card = null;
        if ( "".equals(user_sex) )
            user_sex = null;
        if ( "".equals(user_status) )
            user_status = null;
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> infos = viewMapper.queryPersonnelInfo(user_type, user_no, name, id_card, user_sex, user_status);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(infos);
        if ( infos.size() > 0 ){
            Page<Map<String, Object>> p = new Page<Map<String, Object>>(pageNum, pageSize, (int) pageInfo.getTotal());
            /***
             * 某些必要字段需要体现，但是并未显示
             */
            List<Map<String, Object>> newlist = new ArrayList<>();
            Map<String, Object> new__map;
            for( Map<String, Object> obj : infos ){
                new__map = new HashMap<>();
                new__map.put("type_is", obj.get("TYPE_IS"));
                new__map.put("id_is", obj.get("ID_IS"));
                new__map.put("user_no", obj.get("USER_NO"));
                new__map.put("user_name", obj.get("USER_NAME"));
                new__map.put("user_sex", obj.get("USER_SEX"));
                new__map.put("id_card", obj.get("USER_IDCARD"));
                new__map.put("recent_p", obj.containsKey("RECENT_P")?(String)obj.get("RECENT_P"):"");
                new__map.put("recent_c", obj.containsKey("RECENT_C")?(String)obj.get("RECENT_C"):"");
                new__map.put("recent_a", obj.containsKey("RECENT_A")?(String)obj.get("RECENT_A"):"");
                new__map.put("mobile", obj.containsKey("MOBILE")?(String)obj.get("MOBILE"):"");
                new__map.put("email", obj.containsKey("EMAIL")?(String)obj.get("EMAIL"):"");
                new__map.put("dept", obj.get("DEPARTMENT"));
                new__map.put("user_status", obj.get("USER_STATUS"));
                newlist.add(new__map);
            }
            p.setList(newlist);
//            p.setList(infos);
            msg =  "数据查询成功";
            statusCode = StatusCode.Success._statusCode;
            json.put("page", p);
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }
}
